<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IP地址查询工具</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            width: 100%;
            max-width: 600px;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(90deg, #2575fc 0%, #6a11cb 100%);
            color: white;
            padding: 25px 30px;
            text-align: center;
        }
        
        .header h1 {
            font-weight: 600;
            font-size: 28px;
            margin-bottom: 5px;
        }
        
        .header p {
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .input-group {
            margin-bottom: 25px;
        }
        
        .input-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }
        
        .ip-input {
            width: 100%;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        .ip-input:focus {
            border-color: #2575fc;
            outline: none;
            box-shadow: 0 0 0 3px rgba(37, 117, 252, 0.2);
        }
        
        .btn {
            background: linear-gradient(90deg, #2575fc 0%, #6a11cb 100%);
            color: white;
            border: none;
            padding: 15px 25px;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 14px rgba(37, 117, 252, 0.4);
        }
        
        .btn:active {
            transform: translateY(0);
        }
        
        .result-container {
            margin-top: 30px;
            background: #f8f9fa;
            border-radius: 15px;
            padding: 20px;
            border-left: 5px solid #2575fc;
        }
        
        .result-title {
            font-weight: 600;
            margin-bottom: 15px;
            color: #2575fc;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .result-item {
            display: flex;
            margin-bottom: 12px;
            padding-bottom: 12px;
            border-bottom: 1px solid #eee;
        }
        
        .result-item:last-child {
            margin-bottom: 0;
            padding-bottom: 0;
            border-bottom: none;
        }
        
        .result-label {
            flex: 0 0 100px;
            font-weight: 600;
            color: #555;
        }
        
        .result-value {
            flex: 1;
            color: #333;
        }
        
        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            margin: 20px 0;
            color: #2575fc;
        }
        
        .spinner {
            width: 24px;
            height: 24px;
            border: 3px solid rgba(37, 117, 252, 0.3);
            border-radius: 50%;
            border-top-color: #2575fc;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        .error {
            background: #ffebee;
            color: #d32f2f;
            padding: 15px;
            border-radius: 10px;
            margin-top: 20px;
            border-left: 5px solid #d32f2f;
        }
        
        .footer {
            text-align: center;
            padding: 20px;
            color: #777;
            font-size: 14px;
            border-top: 1px solid #eee;
        }
        
        @media (max-width: 600px) {
            .header h1 {
                font-size: 24px;
            }
            
            .content {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div id="app" class="container">
        <div class="header">
            <h1><i class="fas fa-map-marker-alt"></i> IP地址查询工具</h1>
            <p>快速查询IP地址所在地理位置</p>
        </div>
        
        <div class="content">
            <div class="input-group">
                <label for="ip-address"><i class="fas fa-network-wired"></i> IP地址</label>
                <input 
                    type="text" 
                    id="ip-address" 
                    class="ip-input" 
                    placeholder="请输入IP地址（默认使用当前IP）" 
                    v-model="ipAddress"
                >
            </div>
            
            <button class="btn" @click="fetchIpLocation">
                <i class="fas fa-search"></i> 查询地理位置
            </button>
            
            <div class="loading" v-if="loading">
                <div class="spinner"></div>
                <span>正在查询，请稍候...</span>
            </div>
            
            <div class="error" v-if="error">
                <i class="fas fa-exclamation-circle"></i> {{ error }}
            </div>
            
            <div class="result-container" v-if="result && !loading">
                <div class="result-title">
                    <i class="fas fa-info-circle"></i> 查询结果
                </div>
                
                <div class="result-item">
                    <div class="result-label">IP地址</div>
                    <div class="result-value">{{ result.ip || ipAddress }}</div>
                </div>
                
                <div class="result-item">
                    <div class="result-label">省份</div>
                    <div class="result-value">{{ result.province || '未知' }}</div>
                </div>
                
                <div class="result-item">
                    <div class="result-label">城市</div>
                    <div class="result-value">{{ result.city || '未知' }}</div>
                </div>
                
                <div class="result-item">
                    <div class="result-label">区域编码</div>
                    <div class="result-value">{{ result.adcode || '未知' }}</div>
                </div>
                
                <div class="result-item">
                    <div class="result-label">经纬范围</div>
                    <div class="result-value">{{ result.rectangle || '未知' }}</div>
                </div>
            </div>
        </div>
        
        <div class="footer">
            <p>© 2023 IP地址查询工具 | 使用高德地图API</p>
        </div>
    </div>

    <script>
        const { createApp, ref, onMounted } = Vue;
        
        createApp({
            setup() {
                const ipAddress = ref('');
                const loading = ref(false);
                const error = ref('');
                const result = ref(null);
                
                // 获取用户当前IP
                const getUserIP = async () => {
                    try {
                        const response = await fetch('https://api.ipify.org?format=json');
                        const data = await response.json();
                        ipAddress.value = data.ip;
                    } catch (err) {
                        console.error('获取IP失败:', err);
                        ipAddress.value = '42.81.64.7'; // 默认IP
                    }
                };
                
                // 查询IP地理位置
                const fetchIpLocation = async () => {
                    loading.value = true;
                    error.value = '';
                    result.value = null;
                    
                    try {
                        // 在实际项目中，这里应该调用您的SpringBoot后端
                        // 前端直接调用高德API仅用于演示，实际生产应通过后端调用
                        const ip = ipAddress.value || '42.81.64.7';
                        const apiKey = '7784a0a8b44aa4fcc9bdc34c218c4a0c';
                        const url = `https://restapi.amap.com/v3/ip?ip=${ip}&output=JSON&key=${apiKey}`;
                        
                        const response = await fetch(url);
                        const data = await response.json();
                        
                        if (data.status === '1') {
                            result.value = {
                                ip: ip,
                                province: data.province,
                                city: data.city,
                                adcode: data.adcode,
                                rectangle: data.rectangle
                            };
                        } else {
                            throw new Error(data.info || '查询失败');
                        }
                    } catch (err) {
                        console.error('查询失败:', err);
                        error.value = err.message || '查询IP地理位置时发生错误';
                    } finally {
                        loading.value = false;
                    }
                };
                
                onMounted(() => {
                    getUserIP();
                });
                
                return {
                    ipAddress,
                    loading,
                    error,
                    result,
                    fetchIpLocation
                };
            }
        }).mount('#app');
    </script>
</body>
</html>